//货币格式化
import accounting from 'accounting';

// 全局方法
export default {
    install(Vue, options) {
        //判断是否为空，0或null
        Vue.prototype.isNull0 = function (data, txt) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                if(txt == undefined || txt == 0 || txt == null || txt == ''){
                    val = data;
                }else{
                    val = data + txt;
                }
            }
            return val;
        };

        //判断图片是否为空或null，使用替代图
        Vue.prototype.isNullImg = function (src) {
            let val = '';
            if (src == null || src == '') {
                val = '../../../assets/img/houseList-noData.jpg';
            } else {
                val = src +'?x-oss-process=image/resize,m_fill,h_300,w_500';
            }
            return val;
        };

        //计算美元价钱的亿和万
        Vue.prototype.countUSD_BillionsMillion = function (data) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                if (parseInt(data.toString()).length >= 9) {
                    val = accounting.formatMoney(parseInt(data / 100000000), "$", 2) + '亿';
                } else {
                    val = accounting.formatMoney(parseInt(data / 10000), "$", 0) + '万';
                }
            }
            return val;
        };

        //计算美元价钱的k和M
        Vue.prototype.millionThousand = function (data) {
            let val = '';
            if (data == null || data == 0 || data == '$0' || data == '$-') {
                val = '-';
            }else{
                if (data.toString().length >= 7) {
                    val = accounting.formatMoney(data / 1000000, '$', 2) + 'M'
                } else {
                    val = accounting.formatMoney(data / 1000, '$', 0) + 'k'
                }
            }
            return val;
        };

        //计算RMB价钱的亿和万
        Vue.prototype.countRMB_BillionsMillion = function (data) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                if (parseInt(data * 6.657).toString().length >= 9) {
                    val = accounting.formatMoney(parseInt(data * 6.3239) / 100000000, "约¥ ", 2) + '亿';
                } else {
                    val = accounting.formatMoney(parseInt(data * 6.3239) / 10000, "约¥ ", 0) + '万';
                }
            }
            return val;
        };

        //判断美元价钱信息是否为0或null
        Vue.prototype.isNull0_USD = function (data, txt) {
            let val = '';
            if (data == null || data == 0 || data == '$0' || data == '$-') {
                val = '-';
            } else {
                if (txt == undefined || txt == '') {
                    val = accounting.formatMoney(data, "$ ", 0);
                } else {
                    val = accounting.formatMoney(data, "$ ", 0) + txt;
                }
            }
            return val;
        };

        //判断RMB价钱信息是否为0或null
        Vue.prototype.isNull0_RMB = function (data, txt) {
            let val = '';
            if (data == null || data == 0 || data == '$0' || data == '$-') {
                val = '-';
            } else {
                if (txt == undefined || txt == '') {
                    val = accounting.formatMoney(parseInt(data * 6.3239), "约¥ ", 0);
                } else {
                    val = accounting.formatMoney(parseInt(data * 6.3239), "约¥ ", 0) + txt;
                }
            }
            return val;
        };

        //判断平方英尺是否为0或null
        Vue.prototype.isNull0_sqft = function (data, txt) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                val = data +'平方英尺';
            }
            return val;
        };

        //判断平方米是否为0或null
        Vue.prototype.isNull0_sqmt = function (data, txt) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                val ='约'+ parseInt(data * 0.0929) +'平米';
            }
            return val;
        };



        //学校tab切换的key值翻译
        Vue.prototype.schoolTypeTranslation = function (data){
            let val = '';
            if(data == 'elementary'){
                val = '小学';
            }else if(data == 'middle'){
                val = '初中';
            }else if(data == 'high'){
                val = '高中';
            }
            return val;
        };

        //房源详情页使用——HJ数据图表的tab切换的地区级别key值翻译
        Vue.prototype.regionTranslation = function (data){
            let val = '';
            if(data == 'state'){
                val = '州';
            }else if(data == 'city'){
                val = '城市';
            }else if(data == 'zip'){
                val = '邮编';
            }else if(data == 'county'){
                val = '郡县';
            }else if(data == 'neighbourhood'){
                val = '社区';
            }
            return val;
        };
    }
};
